package com.egov.business.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.egov.business.common.service.SysBaseAPI;
import com.egov.business.system.entity.SysUser;
import com.egov.business.system.model.SysLoginModel;
import com.egov.business.system.service.SysUserServiceImpl;
import com.egov.common.JsonResult;
import com.egov.common.constant.CommonConstant;
import com.egov.common.util.PasswordUtil;
import com.egov.common.util.RedisCacheUtil;
import com.egov.config.shiro.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录操作
 * @author denggang
 * @exception
 * @return
 * @date 2019/7/12 9:55
 */
@RestController
@RequestMapping("/sys")
@Api("用户登录")
@Slf4j
public class LoginController {
	@Autowired
	private SysUserServiceImpl sysUserService;
	@Autowired
	private SysBaseAPI sysBaseAPI;
//	@Autowired
//	private ISysLogService logService;
	@Autowired
    private RedisCacheUtil redisUtil;
	

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ApiOperation("登录接口")
	public JsonResult<JSONObject> login(@RequestBody SysLoginModel sysLoginModel) {
		JsonResult<JSONObject> result = new JsonResult<JSONObject>();
		String userName = sysLoginModel.getUsername();
		String password = sysLoginModel.getPassword();
		SysUser sysUser = sysUserService.getUserByName(userName);
		if(sysUser==null) {
			result.error500("该用户不存在");
			sysBaseAPI.addLog("登录失败，用户名:"+userName+"不存在！", CommonConstant.LOG_TYPE_1, null);
			return result;
		}else {
			//密码验证
			String userPassword = PasswordUtil.encrypt(userName, password, sysUser.getSalt());
			String sysPassword = sysUser.getPassword();
			if(!sysPassword.equals(userPassword)) {
				result.error500("用户名或密码错误");
				return result;
			}
			//生成token
			String token = JwtUtil.sign(userName, sysPassword);
			redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
			 //设置超时时间
			redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME/1000);
			
			JSONObject obj = new JSONObject();
			obj.put("token", token);
			obj.put("userInfo", sysUser);
			result.setData(obj);
			result.success("登录成功");
			sysBaseAPI.addLog("用户名: "+userName+",登录成功！", CommonConstant.LOG_TYPE_1, null);
		}
		return result;
	}
	
	/**
	 * 退出登录
	 * @param userName
	 * @return
	 */
//	@RequestMapping(value = "/logout")
//	public Result<Object> logout(HttpServletRequest request, HttpServletResponse response) {
//		//用户退出逻辑
//		Subject subject = SecurityUtils.getSubject();
//		SysUser sysUser = (SysUser)subject.getPrincipal();
//		sysBaseAPI.addLog("用户名: "+sysUser.getRealname()+",退出成功！", CommonConstant.LOG_TYPE_1, null);
//		log.info(" 用户名:  "+sysUser.getRealname()+",退出成功！ ");
//	    subject.logout();
//
//	    String token = request.getHeader(DefContants.X_ACCESS_TOKEN);
//	    //清空用户Token缓存
//	    redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
//	    //清空用户角色缓存
//	    redisUtil.del(CommonConstant.PREFIX_USER_ROLE + sysUser.getUsername());
//		return Result.ok("退出登录成功！");
//	}
	
	/**
	 * 获取访问量
	 * @return
	 */
//	@GetMapping("loginfo")
//	public Result<JSONObject> loginfo() {
//		Result<JSONObject> result = new Result<JSONObject>();
//		JSONObject obj = new JSONObject();
//		// 获取系统访问记录
//		Long totalVisitCount = logService.findTotalVisitCount();
//		obj.put("totalVisitCount", totalVisitCount);
//		Long todayVisitCount = logService.findTodayVisitCount();
//		obj.put("todayVisitCount", todayVisitCount);
//		Long todayIp = logService.findTodayIp();
//		obj.put("todayIp", todayIp);
//		result.setResult(obj);
//		result.success("登录成功");
//		return result;
//	}

}
